<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of myFunctionsclass
 *
 * @author miguel
 */
class loadLocales {

    public static function validateExcelLocales($line, $dataLoad) {

        $data[$line] = '';

        $columnaA = loadLocales::getColumna($dataLoad['A'], "Channels", "name", "id_channel");
        $columnaB = loadLocales::getColumna($dataLoad['B'], "Customers", "name", "id_customer");
        $columnaC = loadLocales::getColumna($dataLoad['C'], "Formats", "name", "id_format");
        $columnaD = loadLocales::getColumna($dataLoad['D'], "CodeFormats", "name", "id_codeformat");
        $columnaE = loadLocales::getColumna($dataLoad['E'], "Concessions", "name", "id_concession");
        $columnaF = loadLocales::getColumna($dataLoad['F'], "Locations", "name", "id_location");
        $columnaL = loadLocales::getColumna($dataLoad['L'], "Comunas", "name", "id_comuna");
        $columnaM = loadLocales::getColumna($dataLoad['M'], "Provincias", "name", "id_provincia");
        $columnaN = loadLocales::getColumna($dataLoad['N'], "Regiones", "name_short", "id_region");
        $columnaO = loadLocales::getColumna($dataLoad['O'], "Paises", "name", "id_pais");
        $columnaQ = loadLocales::getColumna($dataLoad['Q'], "Clusters", "name", "id_cluster");




        if ($columnaA[0] == 1) { $data[$line] = $data[$line] . 'A,';}
        if ($columnaB[0] == 1) { $data[$line] = $data[$line] . 'B,';}
        if ($columnaC[0] == 1) { $data[$line] = $data[$line] . 'C,';}
        if ($columnaD[0] == 1) { $data[$line] = $data[$line] . 'D,';}
        if ($columnaE[0] == 1) { $data[$line] = $data[$line] . 'E,';}
        if ($columnaF[0] == 1) { $data[$line] = $data[$line] . 'F,';}
        if ($columnaL[0] == 1) { $data[$line] = $data[$line] . 'L,';}
        if ($columnaM[0] == 1) { $data[$line] = $data[$line] . 'M,';}
        if ($columnaN[0] == 1) { $data[$line] = $data[$line] . 'N,';}
        if ($columnaO[0] == 1) { $data[$line] = $data[$line] . 'O,';}
        if ($columnaQ[0] == 1) { $data[$line] = $data[$line] . 'Q,';}
        return $data;
    }

    /**
     * getColumna
     * 
     * Obtiene el ID de la Tabla
     * 
     * @param type $string
     * @param type $table as table name
     * @param type $column as column name
     * @param type $table_id as id table
     * @return type Array()
     */
    public static function getColumna($string, $table, $column, $table_id) {

        $columna = Doctrine_Query::create()
                ->from("$table t")
                ->where("t.$column = ?", array($string))
                ->fetchArray();
        

        if ($columna) {
            return array(0, $columna[0]["$table_id"]);
        } else {
            return array(1, null);
        }
    }
    
    public static function insertExcelLocales($line, $dataLoad) {
        
        $data[$line] = '';

        $columnaA = loadLocales::getColumna($dataLoad['A'], "Channels", "name", "id_channel");
        $columnaB = loadLocales::getColumna($dataLoad['B'], "Customers", "name", "id_customer");
        $columnaC = loadLocales::getColumna($dataLoad['C'], "Formats", "name", "id_format");
        $columnaD = loadLocales::getColumna($dataLoad['D'], "CodeFormats", "name", "id_codeformat");
        $columnaE = loadLocales::getColumna($dataLoad['E'], "Concessions", "name", "id_concession");
        $columnaF = loadLocales::getColumna($dataLoad['F'], "Locations", "name", "id_location");
        $columnaL = loadLocales::getColumna($dataLoad['L'], "Comunas", "name", "id_comuna");
        $columnaM = loadLocales::getColumna($dataLoad['M'], "Provincias", "name", "id_provincia");
        $columnaN = loadLocales::getColumna($dataLoad['N'], "Regiones", "name_short", "id_region");
        $columnaO = loadLocales::getColumna($dataLoad['O'], "Paises", "name", "id_pais");
        $columnaQ = loadLocales::getColumna($dataLoad['Q'], "Clusters", "name", "id_cluster");


        try {

            $tabla = Doctrine::getTable('Stores');
            $tabla = new Stores();

            $tabla->name            = $dataLoad['J'];
            $tabla->id_channel      = $columnaA[1];
            $tabla->id_customer     = $columnaB[1];
            $tabla->id_format       = $columnaC[1];
            $tabla->id_codeformat   = $columnaD[1];
            $tabla->id_concession   = $columnaE[1];
            $tabla->id_location     = $columnaF[1];
            
            $tabla->code_supplier   = ((is_bool($dataLoad['G']))?'':$dataLoad['G']);
            $tabla->code_store      = ((is_bool($dataLoad['H']))?'':$dataLoad['H']);
            
            $tabla->address         = ((is_bool($dataLoad['K']))?'':$dataLoad['K']);
            $tabla->id_comuna       = $columnaL[1];
            $tabla->id_provincia    = $columnaM[1];
            $tabla->id_region       = $columnaN[1];
            $tabla->id_pais         = $columnaO[1];
            $tabla->surface         = ((is_bool($dataLoad['P']))?'':$dataLoad['P']);
            $tabla->id_cluster      = $columnaQ[1];
            
            $tabla->responsiblea    = ((is_bool($dataLoad['R']))?'':$dataLoad['R']);
            $tabla->responsibleb    = ((is_bool($dataLoad['S']))?'':$dataLoad['S']);
            $tabla->responsiblec    = ((is_bool($dataLoad['T']))?'':$dataLoad['T']);
            
            $tabla->manager         = ((is_bool($dataLoad['U']))?'':$dataLoad['U']);
            $tabla->administrator   = ((is_bool($dataLoad['V']))?'':$dataLoad['V']);
            $tabla->average_sales   = ((is_bool($dataLoad['W']))?'':$dataLoad['W']);
            

            $tabla->save();
        } catch (Exception $e) {
            syslog(LOG_ERR, "Error: " . $e->getMessage());
        }

    }
    
}

?>
